package com.imessage.utils;

import java.io.BufferedReader;

import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * 工具类，通过反射机制对对象赋值
 * @author
 */

public class ByteUtil {


	/***
	 * 转化为 unicode编码
	 * @param src
	 * @return
	 */
	public static String toHexString(String src){
		StringBuffer sb=new StringBuffer();

		for(int   i=0;i <src.length();i++) {
			String s=Integer.toHexString((int)src.charAt(i)   &   0xFFFF).toUpperCase();
			for(int j=0;j<4-s.length();j++){
				sb.append("0");
			}
			sb.append(s);
		}
		return sb.toString();
	}



	public static byte[] getByteFormStr(String src,int byteLength){//返回数组指定长度
		byte[] srcByte =src.getBytes();
		byte[] result=new byte[byteLength];
		if(srcByte.length<=byteLength){
			System.arraycopy(srcByte, 0, result, 0, srcByte.length);
		}else{
			System.arraycopy(srcByte, 0, result, 0, byteLength);
		}
		return result;
	}


	public static String getBcd(String srcstr){//将压缩的BCD字符串还原成整数
		StringBuffer sb = new StringBuffer("");
		for (int i = 0; i < srcstr.length(); i++)// 取2个16进制的低4位 例如 ;2
		{
			sb.append(ByteUtil.parse(srcstr.charAt(i)));
		}
		return sb.toString();
	}

	public static String Str(String srcstr) // 将16进制的低4位转成二进制
	{
		StringBuffer sb = new StringBuffer("");
		int len = srcstr.length();
		for (int i = 0; i < len; i++)// 取2个16进制的低4位 例如 ;2
		{
			String str=Integer.toBinaryString((ByteUtil.parse(srcstr.charAt(i))));
			for(int j=0;j<(4-str.length());j++){
				sb.append("0");
			}
			sb.append(str);
		}
		return sb.toString();
	}

	public static boolean compByteArray(byte[] arr1, byte[] arr2) {
		boolean result = true;
		int len = arr1.length;
		if (len != arr2.length)
			return false;
		for (int i = 0; i < len; i++) {
			if (arr1[i] != arr2[i]) {
				return false;
			}
		}
		return result;
	}

	public static byte[] concatByteArray(byte[] dest, byte[] src) {
		int dlen = dest.length;
		int slen = src.length;
		byte[] conArray = new byte[dlen + slen];
		System.arraycopy(dest, 0, conArray, 0, dlen);
		System.arraycopy(src, 0, conArray, dlen, slen);
		return conArray;
	}

	public static byte[] getBufferBySep(InputStream in, byte[] sp) {
		byte[] destBuffer = new byte[2];
		destBuffer[0] = -1;
		int spindex = -1;
		int splen = sp.length;
		byte[] tmpArray = new byte[splen];
		try {
			// ByteBuffer bbuffer = ByteBuffer.allocate(in.available());
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			do {
				br.readLine();
				System.out.println(in.available());
				in.read(tmpArray);
			} while (tmpArray.equals(sp));
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return destBuffer;
	}

	public static void printHexBytes(byte[] bytes) {
		for (int i = 0; i < bytes.length; i++) {
			System.out.printf("0x%02X ", bytes[i]);
		}
	}

	public static String byteHEX(byte ib) {
		char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
				'B', 'C', 'D', 'E', 'F' };
		char[] ob = new char[2];
		ob[0] = Digit[(ib >>> 4) & 0X0F];
		ob[1] = Digit[ib & 0X0F];
		String s = new String(ob);
		return s;
	}

	public static byte HEXbyte(String hex) {
		if(hex.length()==1){
			hex="0"+hex;
		}
		return (byte) (Integer.parseInt("" + hex.charAt(0), 16) << 4 | Integer
				.parseInt("" + hex.charAt(1), 16));
	}

	// 整数到字节数组转换
	public static byte[] int2bytes(int n) {
		byte[] ab = new byte[4];
		ab[3] = (byte) (0xff & n);
		ab[2] = (byte) ((0xff00 & n) >> 8);
		ab[1] = (byte) ((0xff0000 & n) >> 16);
		ab[0] = (byte) ((0xff000000 & n) >> 24);
		return ab;
	}

	// 字节数组到整数的转换
	public static int bytes2int(byte b[]) {
		int s = 0;
		byte temp[] = new byte[4];
		if (b.length == 0) {
			return s;
		} else if (b.length == 4) {
			s = ((((b[0] & 0xff) << 8 | (b[1] & 0xff)) << 8) | (b[2] & 0xff)) << 8
					| (b[3] & 0xff);
			return s;
		} else {
			System.arraycopy(b, 0, temp, temp.length - b.length, b.length);
			s = ((((temp[0] & 0xff) << 8 | (temp[1] & 0xff)) << 8) | (temp[2] & 0xff)) << 8
					| (temp[3] & 0xff);
			return s;
		}
	}

	// 字节转换到字符
	public static char byte2char(byte b) {
		return (char) b;
	}

	private final static byte[] hex = "0123456789ABCDEF".getBytes();

	private static int parse(char c) {
		if (c >= 'a')
			return (c - 'a' + 10) & 0x0f;
		if (c >= 'A')
			return (c - 'A' + 10) & 0x0f;
		return (c - '0') & 0x0f;
	}

	// 从字节数组到十六进制字符串转换
	public static String Bytes2HexString(byte[] b) {
		byte[] buff = new byte[2 * b.length];
		for (int i = 0; i < b.length; i++) {
			buff[2 * i] = hex[(b[i] >> 4) & 0x0f];
			buff[2 * i + 1] = hex[b[i] & 0x0f];
		}
		return new String(buff);
	}

	// 从十六进制字符串到字节数组转换
	public static byte[] HexString2Bytes(String hexstr) {
		byte[] b = new byte[hexstr.length() / 2];
		int j = 0;
		for (int i = 0; i < b.length; i++) {
			char c0 = hexstr.charAt(j++);
			char c1 = hexstr.charAt(j++);
			b[i] = (byte) ((parse(c0) << 4) | parse(c1));
		}
		return b;
	}


	public static void main(String[] args) {
		System.out.println(ByteUtil.Bytes2HexString("测".getBytes()));
	}
}
